package com.whw.fy.service.impl;

import com.whw.fy.entity.Message;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.whw.fy.mapper.ParentMapper;
import com.whw.fy.entity.Parent;
import com.whw.fy.service.ParentService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ParentServiceImpl implements ParentService {

    @Resource
    private ParentMapper parentMapper;

    @Override
    public Message deleteByPrimaryKey(Integer parentId) {
        return parentMapper.deleteByPrimaryKey(parentId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Parent record) {
        return parentMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Parent record) {
        return parentMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer parentId) {
        return Message.success(null).add(parentMapper.selectByPrimaryKey(parentId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Parent record) throws ParseException {
        if (record.getParentBirthtoString() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            record.setParentBirth(simpleDateFormat.parse(record.getParentBirthtoString()));
        }

        return parentMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Parent record) {
        return parentMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message mohuSearchParentName(String parentName) {
        List<Parent> parentList = parentMapper.MohuSearchByParentName(parentName);
        List<String> stringList = new LinkedList<>();
        for (Parent parent : parentList) {
            stringList.add(parent.getParentName() + "<" + parent.getParentCall() + ">");
        }
        return Message.success(null).add(stringList);
    }

    @Override
    public Message selectParentNameAndCall() {
        List<Parent> parentList = parentMapper.selectAll();
        List<Map> mapList = new LinkedList<>();
        for (Parent parent : parentList) {
            HashMap map = new HashMap();
            map.put("parentId", parent.getParentId());
            map.put("String", parent.getParentName() + "<" + parent.getParentCall() + ">");
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectByParentSex(String sex) {
        return Message.success(null).add(parentMapper.selectByParentSex(sex));
    }

    @Override
    public Message selectParentNameAndCallBySex(String sex) {
        List<Parent> parentList = parentMapper.selectByParentSex(sex);
        List<Map> mapList = new LinkedList<>();
        for (Parent parent : parentList) {
            HashMap map = new HashMap();
            map.put("parentId", parent.getParentId());
            map.put("String", parent.getParentName() + "<" + parent.getParentCall() + ">");
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(parentMapper.selectAll());
    }

    @Override
    public int updateBatch(List<Parent> list) {
        return parentMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Parent> list) {
        return parentMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<Parent> list) {
        return parentMapper.batchInsert(list);
    }
}


